import java.util.Arrays;

public class Test {

    //改变原有数组元素的值 奇数位于偶数之前 二分查找 冒泡排序  两数之和  只出现一次的数字  多数元素  存在连续三个奇数的数组

    public static void main1(String[] args) {  //改变原有数组元素的值
        int[] array = {1, 2, 3};
        System.out.println(Arrays.toString(array));
        int[] ret = transform(array);
        System.out.println(Arrays.toString(ret));
    }
    public static int[] transform(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = array[i] * 2;
        }
        return array;
    }


    public static void main2(String[] args) {  //奇数位于偶数之前
        int[] array = {1,9,6,4,5,8};
        int[] arr=new int[array.length];
        int n= array.length;
        int j=0;
        //int z=1;
        for (int i = 0; i < array.length; i++)
        {

            if(array[i]%2==1)
            {
                arr[j]=array[i];
                j++;
            }

            if(array[i]%2==0)
            {
                //arr[array.length-z]=array[i];
                //z++;
                arr[n-1]=array[i];
                n=n-1;
            }
        }
        System.out.println(Arrays.toString(arr));
    }


    public static void main3(String[] args) {//二分查找
        int[] array={1,2,3,4,5,6,7,8,9,10};
        int left=0;
        int right=array.length-1;

        int k=7;
        while(left<=right)
        {
            int mid=(left+right)/2;
            if(array[mid]==k)
            {
                System.out.println(mid);
                break;
            }
            if(array[mid]>k)
            {
                right=mid-1;
            }
            if(array[mid]<k)
            {
                left=mid+1;
            }
        }
    }


    public static void main4(String[] args) { //冒泡排序
        int[] array={10,9,8,7,6,5,4,3,2,1};
        for (int i = 0; i <=array.length-1 ; i++)
        {
            for(int j=0;j< array.length-1-i;j++)
            {
                if(array[j]>array[j+1])
                {
                    int tmp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=tmp;
                }
            }
        }
        System.out.println(Arrays.toString(array));
    }


    public static void main5(String[] args) {//两数之和
        int[] sum={1,5,6,8};
        int target=9;
        for(int i=0;i< sum.length;i++)
        {
            for(int j=0;i<sum.length;j++)
            {
                if(i!=j)
                {
                    if (sum[i] + sum[j] == target) {
                        System.out.println(i + "  " + j);
                        return;

                    }
                }
            }
        }
    }


    public static void main(String[] args) {//两数之和
        int[] sum={1,5,6,8};
        int target=9;
        for(int i=0;i< sum.length;i++)
        {
            for(int j=i+1;i<sum.length;j++)
            {

                    if (sum[i] + sum[j] == target) {
                        System.out.println(i + "  " + j);
                        return;

                    }

            }
        }
    }

    public static void main6(String[] args) {//只出现一次的数字
        int[] arr={4,1,2,1,2};
        int n=singleNumber(arr);
        System.out.println(n);
    }
    public static int singleNumber(int[] num) {

        int ret = 0;
        for (int i : num) {
            ret= ret^i;
        }
        return ret;
    }


    public static void main7(String[] args) {//多数元素
        int[] array={2,2,1,1,1,2,2};
        Fun(array);
    }
    public static void Fun(int[] arr) {
        int a = arr.length / 2;
        int count = 0; // 用来保存当前元素出现次数
        Arrays.sort(arr);//1112222
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] == arr[i-1]) {
                count ++;
            }
            else if (arr[i] != arr[i-1]) {
                count = 1;
            }
            if (count > a) {
                System.out.print(arr[i]);
                return;
            }
        }
    }

    public static void main8(String[] args) {//存在连续三个奇数的数组
        int[] arr={1,2,34,3,4,5,7,23,12};
        boolean ret=Fan(arr);
        System.out.println(ret);
    }
    public static boolean Fan(int[] arr)
    {
        int count=0;
        for(int i=0;i< arr.length;i++)
        {
            if(arr[i]%2!=0)
            {
                count++;
                if(count==3)
                {
                    return true;
                }

            }
            else
                count=0;

        }
        return false;
    }

}
